package com.jucai.pppeiyouclient.adapter;

import android.app.Activity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.bean.FriendGroupBean;
import com.jucai.pppeiyouclient.bean.FriendListBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class FriendGroupAdapter extends RecyclerView.Adapter<FriendGroupAdapter.ViewHolder> {

    private Activity mActivity;
    private ArrayList<FriendGroupBean> mArrayList;

    public FriendGroupAdapter(Activity activity, ArrayList<FriendGroupBean> arrayList) {
        this.mActivity = activity;
        this.mArrayList = arrayList;
    }

    @Override
    public int getItemCount() {
        return mArrayList.size();
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        final FriendGroupBean friendGroupBean = mArrayList.get(position);

        holder.mainRecyclerView.setVisibility(View.GONE);
        holder.groupNameTextView.setText(friendGroupBean.getMember_focus_cate_name());

        try {
            JSONArray jsonArray = new JSONArray(friendGroupBean.getMember_focus_cate_list());
            if (jsonArray.length() != 0) {
                ArrayList<FriendListBean> arrayList = new ArrayList<>();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    FriendListBean friendListBean = new FriendListBean();
                    friendListBean.setMember_id(jsonObject.getString("member_id"));
                    friendListBean.setMember_avatar(jsonObject.getString("member_avatar"));
                    friendListBean.setMember_nickname(jsonObject.getString("member_nickname"));
                    friendListBean.setMember_realname(jsonObject.getString("member_realname"));
                    arrayList.add(friendListBean);
                }
                FriendListAdapter friendListAdapter = new FriendListAdapter(mActivity, arrayList);
                holder.mainRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
                holder.mainRecyclerView.setItemAnimator(new DefaultItemAnimator());
                holder.mainRecyclerView.setAdapter(friendListAdapter);
                holder.mainRecyclerView.setVisibility(View.VISIBLE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        holder.groupNameTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.mainRecyclerView.getVisibility() == View.VISIBLE) {
                    holder.mainRecyclerView.setVisibility(View.GONE);
                } else {
                    holder.mainRecyclerView.setVisibility(View.VISIBLE);
                }
            }
        });

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_friends_group, group, false);
        return new ViewHolder(view);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView groupNameTextView;
        private RecyclerView mainRecyclerView;

        private ViewHolder(View view) {
            super(view);

            groupNameTextView = (TextView) view.findViewById(R.id.group_name);
            mainRecyclerView = (RecyclerView) view.findViewById(R.id.main_recycler_view);

        }

    }

}
